package org.testfire.translator.watij;

import org.testfire.model.TasksType;
import org.testfire.model.TestType;
import org.testfire.model.TestsetType;
import org.testfire.translator.TestFireTranslator;
import org.testfire.translator.TranslatorFactory;
import org.testfire.util.DomUtil;

/**
 * Created by IntelliJ IDEA.
 * User: granum
 * Date: Sep 2, 2007
 * Time: 3:52:34 PM
 *
 */
public class TestToWatijTranslator<T> implements TestFireTranslator<TestType>
{

    /**
     * Translate a 'Test' schema object to a Java method.
     * @return The source code for a valid java method.
     */
    public Object translate( TestsetType testset, TestType test )
    {
        StringBuilder buf = new StringBuilder();
        buf.append( String.format( "/**\n Description:  %1$s \n", test.getDescription() ) );
        if ( test.getForBug() != null && test.getForBug().length() > 0 )
        {
            buf.append( String.format( "* For Bug: %1$s \n", test.getForBug() ) );
        }
        buf.append( String.format( "* Created: %1$s \n", DomUtil.DATE_FORMAT.format( test.getCreated() ) ) );
        buf.append( String.format( "* Description: %1$s \n", test.getDescription() ) );
        buf.append( " * @throws Exception -- \n" );
        buf.append( " */\n" );
        buf.append( String.format( "public void %1$s() throws Exception {\n\t", test.getName() ) );
        if ( test.getName().equals( "setUp" ) || test.getName().equals( "tearDown" ) )
        {
            buf.append( String.format( "super.%1$s();\n\t", test.getName() ) );
        }
        TestFireTranslator translator = TranslatorFactory.get( TasksType.class);
        for ( TasksType at : test.getTasks() )
        {
            buf.append( translator.translate( testset, at) );
        }
        buf.append( "}\n" );
        return buf.toString();
    }
}